package icu.sunway.gushenBlogBackEnd.Controller;

import icu.sunway.gushenBlogBackEnd.Entity.Data.CommentDO;
import icu.sunway.gushenBlogBackEnd.Entity.View.Comment.AddCommentChildVO;
import icu.sunway.gushenBlogBackEnd.Entity.View.Comment.AddCommentRootVO;
import icu.sunway.gushenBlogBackEnd.Entity.View.ResultVO;
import icu.sunway.gushenBlogBackEnd.Service.CommentService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/comment")
public class CommentController {
    final CommentService commentService;

    CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @GetMapping("/list_from_article")
    ResultVO<List<CommentDO>> getCommentListFromArticle(@RequestParam String article_id) {
        return commentService.getCommentListFromArticle(article_id);
    }

    @GetMapping("/count_from_article")
    ResultVO<Long> getCommentCountFromArticle(@RequestParam String article_id) {
        return commentService.getCommentCountFromArticle(article_id);
    }

    @PostMapping("/add_root")
    ResultVO<Boolean> addCommentRoot(@RequestBody AddCommentRootVO addCommentRootVO,
                                     @RequestHeader String token) {
        return commentService.addCommentRoot(
                addCommentRootVO.getArticle_id(),
                addCommentRootVO.getContent(), token
        );
    }

    @PostMapping("/add_child")
    ResultVO<Boolean> addCommentChild(@RequestBody AddCommentChildVO addCommentChildVO,
                                      @RequestHeader String token) {
        return commentService.addCommentChild(
                addCommentChildVO.getArticle_id(),
                addCommentChildVO.getTo_comment_id(),
                addCommentChildVO.getParent_comment_id(),
                addCommentChildVO.getContent(),
                token
        );
    }

    @DeleteMapping("/remove")
    ResultVO<Boolean> removeComment(@RequestParam String id,
                                    @RequestHeader(required = false) String token) {
        return commentService.removeComment(id, token);
    }


}
